var app = getApp()
Page({
  data: {
    tentName: '',
    tentTypePic: '',
    camptitle: '',
    block: false,
    restaurant: false,
    check: true,
    bookingHours: 0,
    campId: 0,
    personNo: 0,
    tentTypeId: 0,
    userId: 0,
    price: 0,
    // ip
    motto: '',
    orderMark: ''
  },
  onLoad: function (options) {
    var tentdata = JSON.parse(options.tantdata)
    console.log(tentdata)
    var that = this
    this.setData(tentdata)
    // this.getIp()
  },
  // 获取ip
  // getIp () {
  //   var that = this
  //   wx.request({  // 获取ip
  //     url: 'http://ip-api.com/json',
  //     success: function (e) {
  //       that.setData({
  //         motto: e.data.query
  //       })
  //     },
  //     complete: function () {
  //       app.setLogCloud()
  //       wx.hideLoading()
  //     }
  //   })
  // },
  // 打电话
  calling: function() {
    wx.makePhoneCall({
      phoneNumber: '18316588252', 
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  Block: function() {
    this.setData({
      block: true
    })
  },
  takeOut: function(){
    this.setData({
      restaurant: false
    })
  },
  Cancel: function() {
    this.setData({
      block: false,
      check: true,
      restaurant: false
    })
  },
  Ok: function () {
    var that = this; 
    this.setData({
      block: false,
    })
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res);
        that.setData({                    
          restaurant: true
        })
      },
      fail: (res) => {
        that.setData({
          restaurant: false,
          check: true
        });    
      }
    })
  },
  // 跳到红包
  toMyPackage: function() {
    wx.navigateTo({
      url: '../myPackage/myPackage',
    })
  },
  // 获取文本域文字
  getRemark (e) {
    this.setData({
      orderMark: e.detail.value
    })
  },
  // 提交订单
  submitOrder: function() {
    var that = this
    wx.request({
      url: `${app.globalData.testApiUrl}/api/wx/order/confirmOrder`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        bookingHours: Math.ceil(that.data.bookingHours),
        campId: that.data.campId,
        personNo: that.data.personNo,
        price: that.data.price,
        tentTypeId: that.data.tentTypeId,
        userId : app.globalData.userId,
        remark: that.data.orderMark
      },
      success: (res) => {
        wx.request({
          url: `${app.globalData.testApiUrl}/api/wx/pay/createOrder`,
          method: 'POST',
          data: {
            appid: app.globalData.appId,
            openid: app.globalData.openId,
            nonceStr: app.randomWord(),
            notifyUrl: `${app.globalData.testApiUrl}/api/wx/pay/notify/order`,
            body: res.data.remark,
            // totalFee: res.data.price, // 分
            totalFee: parseInt(res.data.price * 100),
            tradeType: 'JSAPI',
            outTradeNo: res.data.orderNo,
            spbillCreateIp: '218.16.63.137'
            // spbillCreateIp: app.globalData.ip
          },
          success: (res) => {
            wx.requestPayment({
              provider: 'wxpay',
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.packageValue,
              signType: res.data.signType,
              paySign: res.data.paySign,
              appId: app.globalData.openId,
              success: res => {
                console.log(res)
                wx.redirectTo({
                  url: '/pages/index/index',
                })
              },
              fail: (error) => {
                wx.showToast({
                  title: '取消支付',
                  icon: 'none',
                  duration: 1500
                })
                wx.redirectTo({
                  url: '/pages/index/index',
                })
              }
            })
          },
          fail: (error) => {
            wx.showToast({
              title: '支付错误',
              icon: 'none',
              duration: 1500
            })
            // wx.redirectTo({
            //   url: '/pages/index/index',
            // })
          },
          complete: function (e) {
            app.errorDate.backData = e.data
            app.errorDate.statusCode = e.statusCode
            app.errorDate.route = 'submitOrder'
            app.errorDate.interface = '/api/wx/pay/createOrder'
            app.setLogCloud()
            wx.hideLoading()
          }
        })
      },
      fail: (error) => {
        console.log('订单错误', error)
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'submitOrder'
        app.errorDate.interface = '/api/wx/pay/confirmOrder'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  }
})
